import {
  RECEIVE_ADDRESS,
  RECEIVE_CATEGORIES,
  RECEIVE_SHOPS, RECEIVE_USERINFO ,
  REST_USER_INFO,
  RECEIVE_SHOPGOODS,
  UPDATE_FOOD_COUNT ,
  RECEIVE_INFO,
  CREAR_CART,
  RECEIVE_RATINGS ,
  RECEIVE_SEARCH_SHOPS
} from './mutation-types'
import Vue from 'vue'
export default {
  //第二个参数是对象，获取里面的address属性
  [RECEIVE_ADDRESS](state,payload){
    state.address = payload
  },
  [RECEIVE_CATEGORIES](state,payload){
    state.categories = payload
  },
  [RECEIVE_SHOPS](state,payload){
    state.shops = payload
  },
  [RECEIVE_USERINFO](state,payload){
    console.log(payload)
    state.userinfo = payload.data
  },
  [REST_USER_INFO](state){
    state.userinfo = {}
  },
  [RECEIVE_SHOPGOODS](state,payload){
    console.log(payload)
    state.shopGoods = payload
  },
  [RECEIVE_INFO](state, payload) {
    state.info = payload
  },
  //同步更新food中的count数量
  [UPDATE_FOOD_COUNT](state,{isAdd,food}){
    if(!food.count){
      //food.count = 1
      Vue.set(food,'count',1)   //保证让新增的属性也有数据绑定
      // 将food添加到cartFoods中
      state.cartFoods.push(food)
    } else {
      if (isAdd){
        food.count++
      } else{
        if(food.count){
          food.count--
          if(food.count === 0){
            // 将food从cartFoods中移除
            state.cartFoods.splice(state.cartFoods.indexOf(food),1)
          }
        }
      }
    }
  },
  //同步清空购物车
  [CREAR_CART](state) {
    //清除food中的count (因为关联了很多数组，不能简单的把cartFoods设置为空数组)
    state.cartFoods.forEach(food=>{
      food.count = 0
    })
    //移除购物车中的所有购物项
    state.cartFoods = []
  },
  [RECEIVE_RATINGS](state,payload){
    state.ratings = payload
  },
  [RECEIVE_SEARCH_SHOPS](state,payload){
    state.searchShops = payload
  },
}
